package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.LoginInfoBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IChangeLoginPasswordContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.ChangeLoginPasswordModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/15.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class ChangeLoginPasswordPersenter implements IChangeLoginPasswordContract.IChangeLoginPasswordPersenter {
    private String TAG = ChangeLoginPasswordPersenter.class.getSimpleName();
    private IChangeLoginPasswordContract.IChangeLoginPasswordView mIChangeLoginPasswordView;
    private IChangeLoginPasswordContract.IChangeLoginPasswordModle mIChangeLoginPasswordModle;
    private Handler handler = new Handler();

    public ChangeLoginPasswordPersenter(IChangeLoginPasswordContract.IChangeLoginPasswordView mIChangeLoginPasswordView) {
        this.mIChangeLoginPasswordView = mIChangeLoginPasswordView;
        mIChangeLoginPasswordModle = new ChangeLoginPasswordModle();
    }

    @Override
    public void initTitle() {
        mIChangeLoginPasswordView.showTitle();
    }

    @Override
    public void onClickedDetermine(Context context) {
        String CurrentPassword =  mIChangeLoginPasswordView.getCurrentPassword();
        if(!TextUtils.isEmpty(CurrentPassword)){
            String NewPassword =  mIChangeLoginPasswordView.getNewPassword();
            if(!TextUtils.isEmpty(NewPassword)){
                if(NewPassword.length()<= 16){
                    String ConfirmPassword =  mIChangeLoginPasswordView.getConfirmPassword();
                    if(!TextUtils.isEmpty(ConfirmPassword)){
                        if(ConfirmPassword.length() <= 16){
                            if(NewPassword.equals(ConfirmPassword)){
                                ChangePasswrod(CurrentPassword,NewPassword,context);
                            }else{
                                mIChangeLoginPasswordView.showToastInfo("新密码与确认密码不一致");
                            }
                        }else{
                            mIChangeLoginPasswordView.showToastInfo("确认密码长度超出16");
                        }
                    }else{
                        mIChangeLoginPasswordView.showToastInfo("请输入确认密码");
                    }
                }else{
                    mIChangeLoginPasswordView.showToastInfo("新密码长度超出16");
                }
            }else{
                mIChangeLoginPasswordView.showToastInfo("请输入新密码");
            }
        }else{
            mIChangeLoginPasswordView.showToastInfo("请输入当前密码");
        }
    }

    private void ChangePasswrod(String password, String newPassword, final Context context) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.USER_CHANGE_PASSWORD)
                .addHeader("sessionId",mIChangeLoginPasswordModle.getToken(context))
                .addParams("oldPwd", password)
                .addParams("password", newPassword)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                final String Message = object.getString("message");
                                if(codo == 100){
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mIChangeLoginPasswordView.showToastInfo("修改成功");
                                            // todo 存储是否修改密码成功
                                            mIChangeLoginPasswordModle.SaveIsChangePassword(context);
                                            // 退出当前页面
                                            mIChangeLoginPasswordView.finshPager();

                                        }
                                    });
                                }else{
                                    // todo 切换到主线程
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mIChangeLoginPasswordView.showToastInfo("修改失败");
                                        }
                                    });
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mIChangeLoginPasswordView.showToastInfo("修改失败！");
                            }
                        });
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
